Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SSR hydration performance #6204

Merged
merged 2 commits into from
Apr 21, 2021
Merged

Improve SSR hydration performance #6204

merged 2 commits into from
Apr 21, 2021

Conversation

benmccann
Copy link
Member

@benmccann benmccann commented Apr 17, 2021

Closes #5623. This is that PR rebased with logging removed. Thanks to @jonatansberg for paving the way on this!

Also closes #4309

Background

This PR improves SSR hydration performance through minimizing DOM mutations during hydration by avoiding de- and reattaching nodes during hydration. This should fix #1067, fix #4308, fix #5108, and fix #4975.

In order for this to work I added a new new "hydration mode". This way it's possible to maintain the detach by default behavior during hydration without modifying the generated component output. There might be better ways to do this if we reconsider fragment creation (as suggested in #3898 and #4219) but that would be a significant undertaking.

The existing append, insert and detached methods have been modified to track which nodes are claimed during hydration. That way unclaimed nodes can then removed from the DOM at the end of hydration without touching the remaining nodes.

In order for this to work the append, insert and detach methods have also been made idempotent (i.e. "upserts") in order to allow attaching an already attached DOM node.

Results

Initial local benchmarks against johnells.se (which runs on Sapper) shows a 50% reduction of LCP when running Lighthouse, which results in decent PageSpeed score improvement.

Production benchmarks on the SvelteKit app at https://c3.ventures/ show a PageSpeed score improvement from 92 to 99.

Jonatan Svennberg and others added 2 commits April 21, 2021 09:30
- Fixes #4308 by avoiding de- and reattaching nodes during hydration
- Turns existing append, insert and detach methods into "upserts"

The new "hydration mode" was added in order to maintain the detach by
default behavior during hydration. By tracking which nodes are claimed
during hydration unclaimed nodes can then removed from the DOM at the
end of hydration without touching the remaining nodes.
@benmccann benmccann merged commit 10e3e3d into sveltejs:master Apr 21, 2021
@benmccann benmccann deleted the hydration branch April 21, 2021 16:42
@PierBover
Copy link

Thank you @benmccann and @jonatansberg !

@DhyeyMoliya
Copy link

DhyeyMoliya commented May 1, 2021

This PR creates a weird issue where elements/components are being duplicated on SSR. Possibly while using <svelte:head>. I do not have a reproduction at this moment.

Maybe also links to this issue: #1067

Update : This one includes reproduction: https://github.com/sveltejs/kit/issues/1288

Conduitry added a commit to Conduitry/sveltejs_svelte that referenced this pull request May 3, 2021
@Conduitry Conduitry mentioned this pull request May 3, 2021
4 tasks
hbirler added a commit to hbirler/svelte that referenced this pull request Jun 9, 2021
Unreverts the hydration optimisation from sveltejs#6204 and fixes the `claim_text` issue as described in sveltejs#6279 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hydration speed up React-like efficient hydration Better hydration Hydration Re-Renders Entire DOM
6 participants